<template>
  <base-breadcrumb :breadcrumb="!isAudit">
    <div class="about-master-container" v-loading="loading">
      <a-form-model ref="form" :model="form" :rules="rules">
        <a-card>
          <title-name title="基本信息" />
          <a-row>
            <a-col span="8">
              <a-form-model-item label="盘点计划编号">
                <b-input v-model="form.inventoryPlanCode" disabled />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="项目名称" prop="projectId">
                <SelectProject
                  v-if="!disabled"
                  ref="selectProject"
                  placeholder="请选择"
                  v-model="form.projectId"
                  :form="form"
                  keyValue="branchComId"
                  @selectChange="calcPlanName(1,$event)"
                />
                <b-input v-model="form.projectName" v-else disabled />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="盘点计划名称">
                <b-input v-model="form.inventoryPlanName" disabled />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="盘点编号" prop="inventoryCode">
                <b-input v-model="form.inventoryCode" v-if="!disabled" />
                <b-input v-model="form.inventoryCodeAll" disabled v-else />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="盘点年月" prop="inventoryTime">
                <a-month-picker v-model="form.inventoryTime"
                                @change="calcPlanName(2)"
                                placeholder="加载中"
                                :disabled="disabled" value-format="YYYY-MM"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="创建人">
                <b-input v-model="form.creatorName" disabled />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="创建时间">
                <b-input v-model="form.createTime" disabled />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
        <a-card>
          <title-name title="盘点计划" />
          <a-row>
            <a-col span="8">
              <a-form-model-item label="盘点小组组长" prop="groupLeader">
                <b-input v-model="form.groupLeader" :disabled="disabled" />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="盘点小组组员" prop="groupMember">
                <b-input v-model="form.groupMember" :disabled="disabled" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col span="24">
              <a-form-model-item label="盘点时间" prop="inventoryDate">
                <a-date-picker style="width: 200px;" v-model="form.inventoryDate" value-format="YYYY-MM-DD HH:mm:ss"
                               type="date" :disabled="disabled"
                />
                确保物资成本消耗数据与工程量和收入匹配。
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col span="24">
              <a-form-model-item label="盘点物资">
                <div class="flex" style="flex-wrap: wrap">
                  原材料，材料成品、半成品，未报工程量部位耗用的材料，仓库物资。项目经理
                  <b-input style="width: 200px;margin: 0 10px;" v-model="form.projectManager"
                           class="requiredFormItem" :disabled="disabled"
                  />
                  组织盘点，约定物资部材料主管
                  <b-input style="width: 200px;margin: 0 10px;" v-model="form.materialDirector"
                           class="requiredFormItem" :disabled="disabled"
                  />
                  材料工程师
                  <b-input style="width: 200px;margin: 0 10px;" v-model="form.materialEngineer"
                           class="requiredFormItem" :disabled="disabled"
                  />
                  负责盘点原材料与仓库物资；商务部
                  <b-input style="width: 200px;margin: 0 10px;" v-model="form.commerceDepartment"
                           class="requiredFormItem" :disabled="disabled"
                  />
                  负责确定已完成但未达到报量条件的形象部位盘点及加工场、作 业面的钢筋半成品，施工技术工程师
                  <b-input style="width: 200px;margin: 0 10px;" v-model="form.technologyEngineer"
                           class="requiredFormItem" :disabled="disabled"
                  />
                  组织对现场施工进度情况盘点，财务资金部
                  <b-input style="width: 200px;margin: 0 10px;" v-model="form.financialCapital"
                           class="requiredFormItem" :disabled="disabled"
                  />
                  负责复核实物与账面数据的一致性
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col span="24">
              <a-form-model-item label="盘点记录" prop="directorMaterial">
                <div class="flex">
                  1.材料主管
                  <b-input style="width: 200px;margin: 0 10px;" v-model="form.directorMaterial" :disabled="disabled" />
                  负责组织完成盘点计划、盘点表、盘点差异报告等盘点记录。
                </div>
                2.月度盘点至少要保留盘点原始记录与《材料盘点明细表》对应（CSCEC81-SW-WZ-B032）。<br>
                3.半年、年度盘点表的种类、格式等要求，按公司每年发布的盘点文件执行。
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col span="24">
              <a-form-model-item label="其他要求">
                1.盘点时，根据各种存货的实际库存量与账面库存量核对，禁止以估算代替盘点、以账面数代替实际数。
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="8" :offset="16">
              <a-form-model-item style="display:flex;justify-content: flex-end;text-align: end;">
                {{ form.projectName }}
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="8" :offset="16">
              <a-form-model-item class="requiredFormItem" style="display:flex;justify-content: flex-end;"
                                 prop="planDate"
              >
                <a-date-picker v-model="form.planDate" value-format="YYYY-MM-DD HH:mm:ss" :disabled="disabled" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
      </a-form-model>
      <div class="pub-header-btn page-btn-right-top">
        <template v-if="!isAudit">
          <template v-if="!disabled">
            <a-button type="primary" @click="submit('DRAFT')">保存</a-button>
            <a-button type="primary" @click="submit('SUBMITTED')">提交</a-button>
          </template>
          <template v-else>
            <BasePrintButton
              :before-print-fn="beforePrintFn"
              style="display: inline-block;"
            />
          </template>
        </template>
        <a-button v-checkVisibleInDetail @click="$close">关闭</a-button>
      </div>
    </div>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>
<script>
import BasePrintButton from '@/components/BasePrintButton'
import SelectProject from '@/components/Select/SelectProject'
import Api from '@/api/material/inventory/checkPlan'
import moment from 'moment'

export default {
  name: 'a' + Date.now(),
  components: {
    SelectProject,
    BasePrintButton
  },
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.disabled = true
        }
      }
    }
  },
  data() {
    return {
      beforePrintFn: async () => {
        let res = await Api.print({}, { id: this.form.id })
        return res.data.content
      },
      loading: false,
      disabled: this.$route.query.pageType === 'look',
      form: {
        createTime: this.getNowTime(),
        creatorName: this.$userInfo().name,
        branchComId: this.$currentPost().orgId,
        branchComName: this.$currentPost().orgName
      },
      rules: {
        groupLeader: { required: true, message: '请填写盘点小组组长', trigger: 'blur' },
        groupMember: { required: true, message: '请填写盘点小组组员', trigger: 'blur' },
        directorMaterial: { required: true, message: '请填写材料主管', trigger: 'blur' },
        inventoryCode: { required: true, message: '请填写盘点编号', trigger: 'blur' },
        inventoryDate: { required: true, message: '请选择盘点时间', trigger: 'change' },
        planDate: { required: true, message: '请选择日期', trigger: 'change' },
        projectId: { required: true, message: '请选择项目名称', trigger: 'change' },
        inventoryTime: { required: true, message: '请选择盘点年月', trigger: 'change' }
      },
        //获取流程配置值
        processConfig: null,
        //点击提交发送流程配置值
        setProcessConfig: null,
        businessId: null
    }
  },
  async mounted() {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.getData()
    }
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('material_inventory_plan')
  },
  methods: {
    async getData() {
      this.loading = true
      let res = await Api.findById({}, { id: this.form.id })
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
        this.form = res.data
      }
    },
    calcPlanName(type, e) {
      if (type === 1) {
        this.form.projectName = e.name
      }
      if (!this.form.projectId) return
      if (!this.form.inventoryTime) return
      this.form.inventoryPlanName = `${this.form.projectName}物资${this.form.inventoryTime}盘点计划`
    },
    async check() {
      return new Promise(resolve => {
        this.$refs.form.validate(async valid => {
          if (valid) {
            if (!this.form.projectManager) return this.$noticeWarning('请填写项目经理')
            if (!this.form.materialDirector) return this.$noticeWarning('请填写物资部材料主管')
            if (!this.form.materialEngineer) return this.$noticeWarning('请填写材料工程师')
            if (!this.form.commerceDepartment) return this.$noticeWarning('请填写商务部')
            if (!this.form.technologyEngineer) return this.$noticeWarning('请填写施工技术工程师')
            if (!this.form.financialCapital) return this.$noticeWarning('请填写财务资金部')
            resolve(true)
          } else {
            this.$noticeWarning('请填写完整')
          }
        })
      })
    },
    async submit(status) {
       this.setProcessConfig = null;
      if (status !== 'DRAFT') {
        if (!await this.check()) return
      }

      let temp = this.$clone(this.form)
      temp.auditStatus = status
      let res
      this.loading = true
      this.submitApprovalConfirm(status =='SUBMITTED'?this.processConfig:6,async (flag)=>{
        if(!flag) {
          this.loading = false
          return
        }
      if (temp.id) {
        res = await Api.editByMain(temp)
      } else {
        res = await Api.createByMain(temp)
      }
      this.loading = false
      if (res.code === this.SUCCESS_CODE) {
        this.$noticeSuccess('成功')
        if(status =='SUBMITTED') {
            //提交时，返回id
              this.businessId = res.data.id;
              this.setProcessConfig = this.processConfig;
              return
            }else{
            this.$close()
          }
      }
      })
    }
  }
}
</script>
